import axios from 'axios'
export default {
  namespaced:true,
  state: {
    list:[]
  },
  getters: {
    totalCount(state){
       return state.list.reduce((prev,item)=>prev+=item.count,0)
    },
    totalPrice(state){
      return state.list.reduce((prev,item)=>prev+=(item.count*item.price),0)
    }
  },
  mutations: {
    getList(state,list){
    state.list=list
    }
  },
  actions: {
   async getCartList(context){
   const res = await axios.get('http://localhost:3000/cart')
   console.log(res)
   context.commit('getList',res.data)
    },
  async updateCount(context,data){
    await axios.patch(`http://localhost:3000/cart/${data.id}`,{
      count:data.count
     })
     context.dispatch('getCartList')
   }
  }
}